package com.szholly.pro.watergas_server.ps.android.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.ps.android.bean.QcTaskParam;
import com.szholly.pro.watergas_server.ps.android.common.DateTime;
import com.szholly.pro.watergas_server.ps.android.common.NonNullUtil;
import com.szholly.pro.watergas_server.ps.android.common.SqlUtils;
import com.szholly.pro.watergas_server.ps.android.common.ValidUtils;
import com.szholly.pro.watergas_server.ps.android.controller.QcControl;
import com.szholly.pro.watergas_server.ps.data.OperationTaskRow;
import com.szholly.pro.watergas_server.ps.data.OperationTaskTable;
import com.szholly.utils.util.StringUtils;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.RowBase;

public class QcLabOperationService implements QcOperationService {
	public final static QcLabOperationService INSTANCE = new QcLabOperationService();

	public List<RowBase> getOperationTaskList(QcTaskParam taskParam) throws Exception {
		String userId = taskParam.getUserId();
		String taskTypeCode = taskParam.getTaskTypeCode();
		String companyCode = taskParam.getCompanyCode();
		String pointCode = taskParam.getPointCode();
		String jobCode = QcControl.getTaskGuid(taskTypeCode);
		return queryTaskList(userId, jobCode, null, companyCode, pointCode);
	}

	public RowBase addOperationTask(QcTaskParam taskParam) throws Exception {
		String userId = taskParam.getUserId();
		String taskTypeCode = taskParam.getTaskTypeCode();
		String companyCode = taskParam.getCompanyCode();
		String pointCode = taskParam.getPointCode();
		String jobCode = QcControl.getTaskGuid(taskTypeCode);
		String parentCode = taskParam.getParentTaskCode();
		
		
		String taskCode = "qc" + new DateTime().toString("yyyyMMddHHmmss") + QcOperationService.createSuffix();
		ValidUtils.hasText(userId, "用户编号为空");
		ValidUtils.hasText(taskTypeCode, "任务类型编号为空");
		ValidUtils.hasText(jobCode, "任务编码编号为空");

		OperationTaskRow row = new OperationTaskRow();
		if (!StringUtils.IsNullOrSpace(companyCode)) {
			row.setWwtpId(companyCode);
		}
		if (!StringUtils.IsNullOrSpace(pointCode)) {
			row.setStationId(pointCode);
		}
		row.setStartTime(new Date());
		row.setFinishBy(userId);
		row.setJobId(jobCode);
		row.setTaskCode(taskCode);
		row.setTaskStatus("0");
		row.setParentTaskCode(parentCode);
		SqlUtils.insert(OperationTaskTable.TABLE_NAME, row);
		List<RowBase> taskList = queryTaskList(userId, jobCode, taskCode, companyCode, pointCode);
		ValidUtils.notEmpty(taskList);
		return taskList.get(0);
	}

	/**
	 * 根据站点编号或者公司编号获取运维任务
	 */
	private List<RowBase> queryTaskList(String userId, String jobId, String taskCode, String companyCode, String pointCode) throws Exception {
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		String sql = "select t.TASK_CODE \"id\",\n"
				+ "       case t.Task_Status\n"
				+ "         when '0' then\n"
				+ "          '待处理'\n"
				+ "         else\n"
				+ "          '已处理'\n"
				+ "       end \"name\",\n"
				+ "       t.TASK_DESC \"des\",\n"
				+ "       t.Task_Status \"type\",\n"
				+ "       t.parent_task_code \"parentCode\",\n"
				+ "       to_char(t.START_TIME, 'yyyy-mm-dd hh24:mi:ss') \"startTime\",\n"
				+ "       to_char(t.END_TIME, 'yyyy-mm-dd hh24:mi:ss') \"endTime\",\n"
				+ "       t.FINISH_BY \"userId\"\n"
				+ "  from OP_TASK t\n"
				+ " where 1 = 1\n";
		if (!StringUtils.IsNullOrSpace(companyCode)) {
			parms.add(new GlobalParameter("companyCode", companyCode));
			sql += "   and t.wwtp_id = @companyCode\n";
		}
		if (!StringUtils.IsNullOrSpace(pointCode)) {
			parms.add(new GlobalParameter("pointCode", pointCode));
			sql += "   and t.station_id = @pointCode\n";
		}
		if (!StringUtils.IsNullOrSpace(taskCode)) {
			parms.add(new GlobalParameter("taskCode", taskCode));
			sql += "   and t.task_code = @taskCode\n";
		}
		if (!StringUtils.IsNullOrSpace(jobId)) {
			parms.add(new GlobalParameter("jobId", jobId));
			sql += "   and t.JOB_ID = @jobId\n";
		}
		// if (!StringUtils.IsNullOrSpace(userId)) {
		// parms.add(new GlobalParameter("userId", userId));
		// sql += " and t.FINISH_BY = @userId\n";
		// }
		sql += "order by case\n"
				+ "            when t.end_time is null then\n"
				+ "             t.start_time\n"
				+ "            else\n"
				+ "             t.end_time\n"
				+ "          end desc";
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		return NonNullUtil.toList(rows);
	}

	/**
	 * 完成运维任务
	 */
	public final void preFinishOperationTask(String taskCode, String desc) throws Exception {
		ValidUtils.hasText(taskCode, "任务编号不能为空");
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("taskCode", taskCode));
		parms.add(new GlobalParameter("desc", desc));
		parms.add(new GlobalParameter("type", "1"));
		parms.add(new GlobalParameter("endTime", new DateTime().toString()));
		String sql = "update OPERATION_TASK t set t.task_desc = @desc,t.Task_Status = @type,t.END_TIME =to_date(@endTime,'yyyy-mm-dd hh24:mi:ss') where t.TASK_CODE = @taskCode";
		DataUtil.ExecuteUpdateSql(sql, parms);
	}

	@Override
	public void deleteOperationTask(QcTaskParam taskParam) throws Exception {
		String taskTypeCode = taskParam.getTaskTypeCode();
		String taskCode = taskParam.getTaskCode();
		String jobId = QcControl.getTaskGuid(taskTypeCode);
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("jobId", jobId));
		parms.add(new GlobalParameter("taskCode", taskCode));
		String sql = "delete from OP_TASK t\n" + " where 1 = 1\n" + "   and t.task_code = @taskCode\n" + "   and t.job_id = @jobId";
		DataUtil.ExecuteUpdateSql(sql, parms);
	}
}
